package com.cango.qutz.web.auth.authdept.controller;

import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import com.cango.qutz.web.auth.authdept.service.IAuthDeptService;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.qutz.web.auth.authdept.service.IAuthDeptService;
import com.cango.qutz.web.auth.org.service.IOrgService;
import com.cango.qutz.web.common.BusiConstants;
import com.cango.qutz.web.entity.AuthDeptEntity;
import com.cango.qutz.web.entity.OrgEntity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cango.qutz.web.auth.org.service.IOrgService;
import com.cango.qutz.web.common.BusiConstants;
import com.cango.qutz.web.entity.AuthDeptEntity;
import com.cango.qutz.web.entity.OrgEntity;
import com.cango.framework.base.AjaxReturnVO;
import com.cango.framework.base.controller.BaseController;
import com.cango.framework.base.pagination.DataTableRequest;
import com.cango.framework.base.pagination.DataTableResponse;
import com.cango.framework.base.pagination.PaginationResult;
import com.google.gson.Gson;

/**
 * <p>
 * ClassName: AuthDeptController
 * </p>
 * <p>
 * Description: 部门维护Controller
 * </p>
 * <p>
 * Author: wzx
 * </p>
 * <p>
 * Date: 2015-04-01
 * </p>
 */
@Controller
@RequestMapping("/auth/authdept")
public class AuthDeptController extends BaseController {

    /**
     * log
     */
    private final Log log = LogFactory.getLog(AuthDeptController.class);

    /**
     * <p>
     * Field orgService: 部门Service注入
     * </p>
     */
    @Autowired
    private IAuthDeptService authDeptService;

    /**
     * <p>
     * Field orgService: 机构Service注入
     * </p>
     */
    @Autowired
    private IOrgService orgService;

    /**
     * <p>
     * Description: 部门维护主页面
     * </p>
     *
     * @return 页面
     */
    @RequestMapping("/index")
    public String toAuthDeptIndex() {
        return "auth/authdept/authDept_index";
    }

    /**
     * <p>
     * Description: 到部门列表页面
     * </p>
     *
     * @param orgCode 机构代码
     * @param model   页面接收参数模型
     * @return 页面
     */
    @RequestMapping("/toAuthDeptList")
    public String toAuthDeptList(String orgCode, Model model) {
        // 设置父节点代码
        model.addAttribute("orgCode", orgCode);
        return "auth/authdept/authDept_list";
    }

    /**
     * <p>
     * Description: 得到部门信息的数据
     * </p>
     *
     * @param authDeptSearch 页面查询对象
     * @param dataTable      数据表格对象
     * @return 页面
     */
    @RequestMapping("/getAuthDeptData")
    @ResponseBody
    public DataTableResponse<AuthDeptEntity> getAuthDeptData(
            AuthDeptEntity authDeptSearch,
            DataTableRequest dataTable) {
        PageInfo pageInfo = new PageInfo(dataTable.getCurrentPage(), dataTable.getiDisplayLength());
        PaginationResult<AuthDeptEntity> result = this.authDeptService.queryListByPage(authDeptSearch, pageInfo);
        return new DataTableResponse(dataTable.getsEcho(), result);
    }

    /**
     * <p>
     * Description: 跳转部门编辑页面
     * </p>
     *
     * @param orgCode     机构代码
     * @param operateType 操作类型
     * @param deptId      部门ID（可选）
     * @param model       页面接收参数模型
     * @return 页面
     */
    @RequestMapping("/toEditAuthDept")
    public String toEditAuthDept(String orgCode,
                                 Integer operateType, Model model,
                                 @RequestParam(required = false) Integer deptId) {
        model.addAttribute("orgCode", orgCode);
        model.addAttribute("operateType", operateType);
        String operateName = "";
        //新增与编辑区分
        if (BusiConstants.OPERATE_ADD.equals(operateType)) {
            operateName = "新增";
            OrgEntity orgEntity = this.orgService.queryOrgByCode(orgCode);
            model.addAttribute("orgName", orgEntity.getOrgName());
        } else if (BusiConstants.OPERATE_EDIT.equals(operateType)) {
            operateName = "修改";
            AuthDeptEntity authDept = this.authDeptService.queryObjectById(deptId);
            model.addAttribute("authDept", authDept);
        }
        model.addAttribute("operateName", operateName);
        return "auth/authdept/authDept_edit";
    }

    /**
     * 功能描述: <br>
     * 验证部门代码唯一
     *
     * @param deptCode 部门代码
     * @return 是否唯一
     */
    @RequestMapping("/validDeptCode")
    @ResponseBody
    public boolean validDeptCode(String deptCode) {
        return this.authDeptService.validDeptCode(deptCode);
    }

    /**
     * <p>
     * Description: 新增部门保存
     * </p>
     *
     * @param authDeptEntity 部门对象
     * @param response       响应对象
     */
    @RequestMapping("/saveAuthDeptForAdd")
    public void saveAuthDeptForAdd(AuthDeptEntity authDeptEntity, HttpServletResponse response) {
        log.info("用户" + getSessionUser().getUserName() + "新增了部门:" + (new Gson()).toJson(authDeptEntity));
        authDeptEntity.setCreateUser(getSessionUser().getUserId());
        authDeptEntity.setCreateTime(new Date());
        this.authDeptService.insert(authDeptEntity);
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * <p>
     * Description: 修改部门保存
     * </p>
     *
     * @param authDeptEntity 机构对象
     * @param response       响应对象
     */
    @RequestMapping("/saveAuthDeptForEdit")
    public void saveAuthDeptForEdit(AuthDeptEntity authDeptEntity, HttpServletResponse response) {
        authDeptEntity.setUpdateUser(getSessionUser().getUserId());
        this.authDeptService.update(authDeptEntity);
        log.info("用户" + getSessionUser().getUserName() + "修改了部门:" + (new Gson()).toJson(authDeptEntity));
        super.ajaxJson(response, AjaxReturnVO.success());
    }

    /**
     * <p>
     * Description: 删除部门记录
     * </p>
     *
     * @param deptId   部门ID
     * @param response 响应对象
     */
    @RequestMapping("/deleteAuthDept")
    public void deleteAuthDept(Integer deptId, HttpServletResponse response) {
        AuthDeptEntity authDeptEntity = new AuthDeptEntity();
        authDeptEntity.setDeptId(deptId);
        authDeptEntity.setUpdateUser(getSessionUser().getUserId());
        this.authDeptService.delete(authDeptEntity);
        log.info("用户" + getSessionUser().getUserName() + "删除了部门:" + (new Gson()).toJson(authDeptEntity));
        super.ajaxJson(response, AjaxReturnVO.success());
    }
}
